//
//  ViewController.m
//  Example
//
//  Created by 九爷 on 2024/1/16.
//

#import "ViewController.h"

#import <JYTMacros.h>
#import <LYExtension/LYExtension.h>
#import <JYTNFCCore/JYTNFCCore.h>

@interface ViewController ()

@property (nonatomic, strong) JYTNFCOperate *opt;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
//    [NSObject saveKeychainWithObject:@"1723502907" service:kBundleIdentifier key:@"nfc_time"];
//    [NSObject removeKeychainObjectWithService:kBundleIdentifier key:@"nfc_time" error:nil];
//    [NSObject removeKeychainObjectWithService:kBundleIdentifier key:@"nfc_token" error:nil];
    [JYTNFCOperate isDebugEnv:YES];
    
//    [NSObject saveKeychainWithObject:@"1724383324" service:kBundleIdentifier key:@"nfc_time"];
    
    NSString *json = @"{\"apdusResult\": {\"balance\": \"00004E209000\",\"creditCappRecordInfo\": \"062E001500204E0000204E000002402700010001202407171643426472140000000000000000000000000000000000009000\",\"creditInit\": \"00004E200016010010F980AD8C459CE4\",\"ep05\": \"1000000000000001000000000000004027000000000000000000000000202312290000000000000000000000000000009000\",\"ep16\": \"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009000\",\"epCreditInit\": \"00004E200016010041FCC065DFFF5A829000\",\"epPurchaseInit\": \"00004E2000120000000100074358979000\",\"ep_record_01\": \"001600000000004E2002402700010001202407171643429000\",\"ep_record_02\": \"001200000000004E2009402700010001202407171643429000\",\"ep_record_03\": \"001500000000004E2002402700010001202407171630429000\",\"ep_record_04\": \"001100000000004E2009402700010001202407171630429000\",\"ep_record_05\": \"001400000000004E2002402700010001202407171630019000\",\"ep_record_06\": \"001000000000004E2009402700010001202407171630019000\",\"ep_record_07\": \"001300000000004E2002402700010001202407171538589000\",\"ep_record_08\": \"000F00000000004E2009402700010001202407171538589000\",\"ep_record_09\": \"001200000000004E2002402700010001202407171536039000\",\"ep_record_0A\": \"000E00000000004E2009402700010001202407171536039000\",\"mfname\": \"9000\",\"pubInfo\": \"01396900FFFFFFFF010103104920100000001179202407102024072010019000\",\"selectEP\": \"6F318408A000000632010105A5259F0801029F0C1E01396900FFFFFFFF010103104920100000001179202407102024072010019000\",\"tcCreditInit\": \"0000000000000100D4C6FFEA752E207E9000\",\"tcInfo\": \"00010000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000009000\",\"tcPurchaseInit\": \"000000000000000000010027B770EE9000\",\"ypbalance\": \"000000009000\"},\"ats\": \"4D54009000200090ED0888BC\",\"customer_code\": \"chongqingcity\",\"dataType\": \"00\",\"operator_no\": \"nfc\",\"params\": {},\"process\": \"except_revert\",\"result\": {},\"result_code\": \"\",\"result_msg\": \"\",\"seq_no\": \"2469\",\"sign_type\": \"md5\",\"tenantCode\": \"004000\",\"terminal_no\": \"999999999999\",\"timestamp\": \"20240718155327\",\"trans_data\": {\"cardUid\": \"ED0888BC\",\"oper\": \"ExceptRevert\",\"physNo\": \"00200090ED0888BC\",\"standard\": \"CQCITY_JTB\",\"tradeTime\": \"20240718155331\"},\"uid\": \"ED0888BC\",\"version\": \"2.0\"}";
//    
//    NSLog(@"%@", [self signStr:[json jsonStrToObject]]);
}

- (NSString *)sortParam:(NSDictionary *)orginParam {
    if ([orginParam allKeys].count == 0) {
        return @"{}";
    }
    NSMutableString *contentString = [NSMutableString string];
    NSArray *keys = [orginParam allKeys];
    //按字母顺序排序
    NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2];
    }];
    
    for (NSString *key in sortedArray) {
        [contentString appendFormat:@"\"%@\":\"%@\",", key, orginParam[key]];
    }
    
    [contentString replaceCharactersInRange:NSMakeRange(contentString.length - 1, 1) withString:@""];
    return [NSString stringWithFormat:@"{%@}", contentString];
}

- (NSString *)signStr:(NSMutableDictionary*)dict {
    NSMutableString *contentString = [NSMutableString string];
    NSArray *keys = [dict allKeys];
    //按字母顺序排序
    NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
    }];
    //拼接字符串
    for (NSString *categoryId in sortedArray) {
        if ([[dict objectForKey:categoryId] isKindOfClass:NSString.class]) {
            [contentString appendFormat:@"%@=%@&", categoryId, [NSString lyFormatStr:[dict objectForKey:categoryId]]];
        } else {
            if ([[dict objectForKey:categoryId] isKindOfClass:NSDictionary.class]) {
                [contentString appendFormat:@"%@=%@&", categoryId, [self sortParam:[dict objectForKey:categoryId]]];
            }
        }
    }
    [contentString appendFormat:@"key=%@", @"chezaijitogprs"];
    NSLog(@"%@", contentString);
    return [[contentString copy] md5String];
}

- (IBAction)shownfc:(id)sender {
    [self.opt jytReadCard:^(BOOL isSuccess, NSString *errorStr, NSString *cardNo, NSString *balance, NSString *cityCode, BOOL isNormalCard, NSString *cardStatusStr, BOOL userCancel) {
        NSLog(@"状态: %@", @(isNormalCard));
        NSLog(@"卡号: %@", cardNo);
        NSLog(@"余额: %@", balance);
        NSLog(@"城市: %@", cityCode);
        NSLog(@"状态描述: %@", cardStatusStr);
    }];
    
    //3104920100000001179 4200001314202112080563290631
    //4027100000213432 4200001200202112035655185850
    
//    [self.opt jytWriteCardWithRechargeFee:@"0.01" orderId:@"4200002431202409049989826797" cardNo:@"3104920140270186173" complation:^(BOOL isSuccess, NSString *errorStr, NSString *balance, BOOL isGaryTrade, NSString *chargeFee, BOOL userCancel) {
//        
//    }];
//    
//    [self.opt jytRevertCard:^(BOOL isSuccess, NSString *errorStr, NSString *balance, BOOL isGaryTrade, NSString *chargeFee) {
//        
//    }];
}

- (JYTNFCOperate *)opt {
    if (!_opt) {
        _opt = JYTNFCOperate.new;
    }
    return _opt;
}

@end
